<?php
defined("_JEXEC") or die("Restricted access");

jimport("joomla.application.component.controller");

class JoomlaIniTranslatorController extends JController
{
	function display() {
		// 直接获取数据
		$model = $this->getModel();
		$items = $model->getData();
		
		// 检测翻译状态 status
		foreach ($items as &$i) {
			$sourceFilePath = $i->langDir . DS . $i->sourceLangDir;
			$targetFilePath = $i->langDir . DS . $i->targetLangDir;
			$languageDirectory = JPATH_ADMINISTRATOR . DS . "language";
			require_once(JPATH_COMPONENT_ADMINISTRATOR . DS . "libraries" . DS . "core" . DS . "lib" . DS . "IniParser.php");
			$parser = new IniParser($i->srcLangCode, $i->srcLangDir, $i->tarLangCode, $i->tarLangDir, JPATH_ADMINISTRATOR . DS . "language");
			
			$diffFileList = $parser->getDiffFiles();
			
			if ($diffFileList === 0) {
				// STATUS 标记翻译完成
				require_once JPATH_COMPONENT_ADMINISTRATOR . DS . "tables" . DS . "joomlainitranslator.php";
				if (!$model->changeStatus($i, TableJoomlaIniTranslator::JIT_STATUS_DONE)) {
					$msg = JText::_("Failed to change status, some thing trouble!<br />".$model->getError());
					$type = "error";
				}
			} else {
				// 有文件需要翻译
				require_once JPATH_COMPONENT_ADMINISTRATOR . DS . "tables" . DS . "joomlainitranslator.php";
				if (!$model->changeStatus($i, TableJoomlaIniTranslator::JIT_STATUS_NEW)) {
					$msg = JText::_("Failed to change status, some thing trouble!<br />".$model->getError());
					$type = "error";
				}
			}
			
			if (strlen($msg) > 0 && $type == "error") {
				$url = 'index.php?option=com_joomlainitranslator';
				$this->setRedirect($url, $msg, $type);
			}
		}
		
		parent::display();
	}
}
?>